from datetime import datetime

from sqlalchemy import BigInteger, Column, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column, TEXT, Double, TIMESTAMP, VARCHAR
from sqlalchemy import PrimaryKeyConstraint

from sqlalchemy import Column, DateTime


# path = os.path.dirname(os.path.abspath(__file__))
# SQLALCHEMY_DATABASE_URL = "sqlite:///./geetest.db"
# SQLALCHEMY_DATABASE_URL = 'postgresql+psycopg2://dbuser_dba:DBUser.DBA@192.168.174.137:5432/data'
# SQLALCHEMY_DATABASE_URL = 'postgresql://dbuser_dba:DBUser.DBA@192.168.174.137:5432/data'
SQLALCHEMY_DATABASE_URL = "mysql+pymysql://root:qiuwen123456@1.15.241.228/data"


engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={})
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
BaseModel = declarative_base()


class SensorData(BaseModel):
    __tablename__ = "sensor_data"  # 设置表明

    id = Column(BigInteger, primary_key=True)
    time = Column(TIMESTAMP)
    location = Column(TEXT, nullable=False, comment="位置")
    temperature = Column(Double, nullable=True)
    humidity = Column(Double, nullable=True)
    pressure = Column(Double, nullable=True)
    PrimaryKeyConstraint("time", "location", name="time_location")


class ShareData(BaseModel):
    __tablename__ = "share_data"  # 设置表名
    id = Column(BigInteger, primary_key=True)
    title = Column(TEXT, nullable=False, comment="分享标题")
    share_link = Column(TEXT, nullable=False, comment="分享链接")
    share_content = Column(TEXT, nullable=True, comment="分享内容")
    share_note = Column(TEXT, nullable=True, comment="备注信息")
    share_desc = Column(TEXT, nullable=True, comment="章节内容")

    pay_link = Column(TEXT, nullable=True, comment="付费链接")

    source_share_link = Column(TEXT, nullable=True, comment="来源地址")
    source_share_note = Column(TEXT, nullable=True, comment="来源地址备注")
    create_time = Column(DateTime, nullable=True, default=datetime.now)
    update_time = Column(
        DateTime, nullable=True, default=datetime.now, onupdate=datetime.now
    )


class v3pic(BaseModel):
    __tablename__ = "v3pic"  # 设置表名
    id = Column(BigInteger, primary_key=True)
    name = Column(VARCHAR(255), nullable=True, comment="name")
    level = Column(VARCHAR(255), nullable=True, comment="level")
    style = Column(VARCHAR(255), nullable=True, comment="style")
    lang = Column(VARCHAR(255), nullable=True, comment="style")
    pic_type = Column(VARCHAR(255), nullable=True, comment="style")
    wordlib = Column(VARCHAR(255), nullable=True, comment="style")
    collection = Column(VARCHAR(255), nullable=True, comment="style")
    answer_text = Column(VARCHAR(255), nullable=True, comment="style")
    answer_location = Column(VARCHAR(255), nullable=True, comment="style")
    prefix_path = Column(VARCHAR(255), nullable=True, comment="style")
